﻿using Microsoft.Extensions.Configuration.Json;

namespace Microsoft.Extensions.DependencyInjection
{
    public static class ConfigurtaionServices
    {
        private static IConfiguration Configuration { get; set; }
        public static IConfiguration GetConfigurtaion(this IServiceCollection containerRegistry)
        {
            //ReloadOnChange = true 当appsettings.json被修改时重新加载            
            Configuration = new ConfigurationBuilder()
            .Add(new JsonConfigurationSource { Path = "appsettings.json", ReloadOnChange = true })
            //根据环境变量读取配置
            //Add(new JsonConfigurationSource { Path = $"appsettings.{System.Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json", ReloadOnChange = true })
            .Build();
            return Configuration;
        }

        /// <summary> 
        /// 通过强类型绑定读取配置 
        /// </summary> 
        /// <typeparam name="Entity">要绑定的类型</typeparam> 
        /// <param name="keyPath">配置文件路径</param> 
        /// <returns>绑定的类</returns> 
        public static Entity GetSectionObject<Entity>(this IServiceCollection containerRegistry,string keyPath = null) where Entity : new()
        {
            var entity = new Entity();
            if (string.IsNullOrEmpty(keyPath))
            {
                //将appsettings.json全部配置绑定到模型  
                Configuration.Bind(entity);
            }
            else
            {
                //将指定路径下的配置项绑定到模型 
                var section = Configuration.GetSection(keyPath);
                section.Bind(entity);
            }
            return entity;
        }
    }
}
